<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑用户</title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://pic2.58.com/n/tuan/js/jquery-1.4.2.min.js"></script>
</head>
<body>
<script >
var functions = ","; // 用户菜单权限
var opers = ","; // 用户操作权限
var roles = ""; // 角色
var rolefuncs = ""; // 角色菜单权限
var roleOpers = ""; // 角色操作权限

function checkAll() {
	var flag = false;

	var username = $("#username").val();
	var realname = $("#realname").val();
	var department = $("#department").val();

	if (username == null || username == "") {
		alert("请填写用户名称！");
	} else if (realname == null || realname == "") {
		alert("请填写真实姓名！");
	} else if (department == null || department == "") {
		alert("请填写所在部门！");
	} else if (functions == null || functions == "") {
		alert("请添加权限!");
	} else {
		flag = true;
	}
	return flag;
}

function checkSelectAll() {
	$(".selectAll").each(function() {
				var f = true;
				$(this).nextAll(':checkbox').each(function() {
					if ($(this).attr("checked") == false){
						f = false;
					}
								
				});
				$(this).attr("checked", f);
			});
}

$(function() {
			functions += $("#userfunction").attr("value") + ",";
			opers += $("#useropers").attr("value") + ",";

			$("#role input:checkbox[checked]").each(function() {

						var id = $(this).attr("id");
						var f = $(this).attr("func");
						var oper = $(this).attr("opers");
						rolefuncs += f;
						roleOpers += oper;
						if (roles == "") {
							roles += id;
						} else {
							roles += "," + id;
						}
					});


			var menusids = rolefuncs.split(",");
			for (var i = 0; i < menusids.length; i++) {
				var menuid = "#function_" + menusids[i];
				var a = "," + menusids[i] + ",";
				if (functions.indexOf(a) >= 0) {
					functions = functions.replace(eval("/" + a + "/g"), ",");
				}
				$(menuid).attr("checked", true);
				$(menuid).attr("disabled", true);
			}


			var operids = roleOpers.split(",");
			for (var i = 0; i < operids.length; i++) {
				var operid = "#opers_" + operids[i];
				var a = "," + operids[i] + ",";
				if (opers.indexOf(a) >= 0) {
					opers = opers.replace(eval("/" + a + "/g"), ",");
				}
				$(operid).attr("checked", true);
				$(operid).attr("disabled", true);
			}

			checkSelectAll();
		});

$(function() {

			$("#role :checkbox").click(function() {
				if ($(this).attr("checked") == false){
					$(this).removeAttr("checked");
				}else{
					$(this).attr("checked", true);
					
				}
				
				$(".menupri input:checkbox[disabled]").each(function() {
						$(this).removeAttr("disabled");
						$(this).removeAttr("checked");
				});	
				
				
				$(".operlist input:checkbox[disabled]").each(function() {
						$(this).removeAttr("disabled");
						$(this).removeAttr("checked");
				});	

				roles = "";
				rolefuncs = "";
				roleOpers = "";
				$("#role input:checkbox[checked]").each(function() {
							var id = $(this).attr("id");
							var f = $(this).attr("func");
							var oper = $(this).attr("opers");
							rolefuncs += f;
							roleOpers += oper;
							if (roles == ""){
								roles += id;
							}else{
								roles += "," + id;
							}
				});
				
				var menusids = rolefuncs.split(",");
				for (var i = 0; i < menusids.length; i++) {
					var menuid = "#function_" + menusids[i];
					var a = "," + menusids[i] + ",";
					if (functions.indexOf(a) >= 0) {
						functions = functions.replace(eval("/" + a + "/g"), ",");
					}
					$(menuid).attr("checked", true);
					$(menuid).attr("disabled", true);
				};
				
				var operids = roleOpers.split(",");
				for (var i = 0; i < operids.length; i++) {
					var operid = "#opers_" + operids[i];
					var a = "," + operids[i] + ",";
					if (opers.indexOf(a) >= 0) {
						opers = opers.replace(eval("/" + a + "/g"), ",");
					}
					$(operid).attr("checked", true);
					$(operid).attr("disabled", true);
				};
				
				checkSelectAll();
			});

			$(".menupri input:checkbox.selectOne").click(function() {
				var id = $(this).attr("value");
				var a = "," + id + ",";
				if ($(this).attr("disabled") == false) {
					if ($(this).attr("checked") == false) {
						$(this).prevAll(".selectAll").attr("checked", false);
						functions = functions
								.replace(eval("/" + a + "/g"), ",");
					} else {
						if (functions.indexOf(a) < 0) {
							functions = functions + id + ",";
						}
						var f = true;
						$(this).prevAll(".selectAll").nextAll(":checkbox")
								.each(function() {
											if ($(this).attr("checked") == false)
												f = false;
										});
						$(this).prevAll(".selectAll").attr("checked", f);
					}
				}
			});

			$(".menupri input:checkbox.selectAll").click(function() {
				var flag = $(this).attr("checked");
				$(this).nextAll(':checkbox').attr("checked", flag);
				$(this).nextAll(':checkbox').each(function() {
					var id = $(this).attr("value");
					var a = "," + id + ",";
					var tmp = rolefuncs + ",";
					if (flag && functions.indexOf(a) < 0 && tmp.indexOf(a) < 0)
						functions = functions + id + ",";
					else if (flag == false) {
						functions = functions.replace(eval("/" + a + "/g"), ",");
						if (tmp.indexOf(a) >= 0){
							$(this).attr("checked", true);
						}
					}
				});
			});

			$(".operlist input:checkbox.selectOne").click(function() {
				var id = $(this).attr("value");
				var a = "," + id + ",";
				if ($(this).attr("disabled") == false) {
					if ($(this).attr("checked") == false) {
						$(this).prevAll(".selectAll").attr("checked", false);
						opers = opers.replace(eval("/" + a + "/g"), ",");
					} else {
						if (opers.indexOf(a) < 0) {
							opers = opers + id + ",";
						}
						var f = true;
						$(this).prevAll(".selectAll").nextAll(":checkbox").each(function() {
							if ($(this).attr("checked") == false){
								f = false;
							}
												
						});
						$(this).prevAll(".selectAll").attr("checked", f);
					}
				}
			});

			$(".operlist input:checkbox.selectAll").click(function() {
				var flag = $(this).attr("checked");
				$(this).nextAll(':checkbox').attr("checked", flag);
				$(this).nextAll(':checkbox').each(function() {
							var id = $(this).attr("value");
							var a = "," + id + ",";
							if (flag && opers.indexOf(a) < 0){
								opers = opers + id + ",";
							}else if (flag == false) {
								opers = opers.replace(eval("/" + a + "/g"), ",");
							}
						});
			});

		});

$(function() {
			$("#toadd").click(function() {
				if (checkAll()) {
					jQuery.ajax({
								url : "/usermanager/toedituser",
								type : "post",
								async : true,
								data : {
									"id" : $("#userid").val(),
									"username" : $("#username").val(),
									"pwd" : $("#pwd").val(),
									"realname" : $("#realname").val(),
									"department" : $("#department").val(),
									"roles" : roles,
									"functions" : functions,
									"userHasedRoles" : $("#userHasedRoles").val(),
									"userfunction" : $("#userfunction").val(),
									"opers" : opers,
									"oldOpers" : $("#useropers").val()
								},
								success : function(data) {
									if (data == "success") {
										alert("添加成功！");
									} else {
										alert("添加失败！");
									}
								}
							});
				}
			})
		});

</script>
<div class="lr_both">
<div class="right_conts">
  <div class="mg_tt">
    <div class="right_search">
      <div class="rs_title_bg">
        <h2 class="rs_title">编辑用户</h2>
      </div>
	  
    </div>
	</br>
	</br>
	<form >
    <div class="table_div" align="center">
	  <input type="hidden" id="userid" name="userid" value="$id"/>
	  <input type="hidden" name="userHasedRoles"  id="userHasedRoles" value="$checkedRoles"/>
	  <input type="hidden"  name="userfunction" id="userfunction" value="$!{userfunction}"/>
	  <input type="hidden"  name="useropers" id="useropers" value="$!{useropers}"/>
      <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
          <th align="center" colspan="2">用户基本信息</th>
		</tr>
		
        <tr>
           <td align="center" >用户名称</td>
		  <td align="center"  ><input type="text"  name="username" id="username" value="$user.username"/></td>
        </tr>
		  <tr>
           <td align="center" >真实姓名</td>
		  <td align="center" ><input type="text" name="realname" id="realname" value="$user.realname"/></td>
        </tr>
		
		  <tr>
           <td align="center">所在部门</td>
		  <td align="center" ><input type="text" name="department" id="department" value="$user.department"/></td>
        </tr>
	 </table>
	 </br>
	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">角色</th>
		  </th>
		</tr>
		
		
		 <tr>
			 <td align="left" id="role">
				#foreach($r in $roleList)
                    <input type="checkbox" name="$r.id" id="$r.id" value="$r.rolename" func="$roleMap.get($r.id)" opers="$roleOperMap.get($r.id)" #if($tools.checkRole($checkedRoles,$r.id)) checked #end #if($tools.checkPublic($r.rolename)) checked disabled #end>&nbsp $r.rolename</input>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
		</tr>
		
	</table>
		</br>
	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">菜单权限</th>
		  </th>
		</tr>
		#foreach($key in $menuMap.keySet())
		 <tr class="menupri">
			#set($cate1 = $key.split('-')[0])
			#set($cate2 = $key.split('-')[1])
             <td align="center" width ="20%"><span style="color:red">$cate1</span> </br> <span style="color:blue">$cate2</span></td>
			 <td align="left" >
			 	<input type="checkbox" class="selectAll">&nbsp 全选 </input>&nbsp;&nbsp;
				#foreach($menu in $menuMap.get($key))
                    <input type="checkbox" class="selectOne" name="function_$menu.id" id="function_$menu.id" value="$menu.id" #if($tools.checkFunction($userfunction,$menu.id)) checked #end>&nbsp $menu.name</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
		</tr>
		#end
      </table>
	  </br>
  
  	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">数据操作细粒度权限</th>
		  </th>
		</tr>
		
		#foreach($key in $operMenuMap.keySet())
		 <tr class="operlist">
			 #set($cate1 = $key.split('-')[0])
		  	 #set($cate2 = $key.split('-')[1])
             <td align="center" width ="20%"><span style="color:red">$cate1</span> </br> <span style="color:blue">$cate2</span></td>
			 <td align="left" >
			 	<input type="checkbox" class="selectAll">&nbsp 全选 </input>&nbsp;&nbsp;
				#foreach($oper in $operMenuMap.get($key))
                    <input type="checkbox" class="selectOne" name="opers_$oper.id" id="opers_$oper.id" value="$oper.id" #if($tools.checkFunction($useropers,$oper.id)) checked #end >&nbsp $oper.name</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
			 
		</tr>
		#end
      </table>
      
	  <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
          <th align="center" colspan="2">
            <input type="button"  id="toadd" name="toadd"  value="保存"/>
			&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;
			<input type="reset"    value="重置"/>
		  </th>
		</tr>
      </table>
    </div>
	</form>
  </div>

</div>
</div>
</body>
</html>
